module Fastlane
  module Actions
    module SharedValues
      PGY_APP_INFOMATION_CUSTOM_VALUE = :PGY_APP_INFOMATION_CUSTOM_VALUE
    end

    class PgyAppInfomationAction < Action
      def self.run(params)
        pgyAppKey = params[:pgy_app_key]
        pgyApiKey = params[:pgy_api_key]
        pgy_params = {'appKey':pgyAppKey,'_api_key':pgyApiKey}
        pgy_uri = URI.parse("http://www.pgyer.com/apiv2/app/view")
        pgy_res = Net::HTTP.post_form(pgy_uri, pgy_params)
        UI.message pgy_res
        pgy_resbody = JSON.parse(pgy_res.body)
        pgy_data = pgy_resbody["data"]
        puts "app 信息：#{pgy_data}"
        return pgy_data
      end

      #####################################################
      # @!group Documentation
      #####################################################

      def self.description
        "A short description with <= 80 characters of what this action does"
      end

      def self.details
        # Optional:
        # this is your chance to provide a more detailed description of this action
        "You can use this action to do cool things..."
      end

      def self.available_options
        # Define all options your action supports.

        # Below a few examples
        [
          FastlaneCore::ConfigItem.new(key: :pgy_app_key,
                                       description: "蒲公英 appKey",
                                       optional: true,
                                       is_string: true),
          FastlaneCore::ConfigItem.new(key: :pgy_api_key,
                                       description: "蒲公英 apiKey",
                                       optional: true,
                                       is_string: true),
        ]
      end

      def self.output
        # Define the shared values you are going to provide
        # Example
        [
          ['PGY_APP_INFOMATION_CUSTOM_VALUE', 'A description of what this value contains']
        ]
      end

      def self.return_value
        # If your method provides a return value, you can describe here what it does
      end

      def self.authors
        # So no one will ever forget your contribution to fastlane :) You are awesome btw!
        ["芝加哥兔兔养殖场"]
      end

      def self.is_supported?(platform)
        # you can do things like
        #
        #  true
        #
        #  platform == :ios
        #
        #  [:ios, :mac].include?(platform)
        #

        platform == :ios
      end
    end
  end
end
